package com.bxs.controller;

import com.bxs.IdsManager;
import com.bxs.global.JsonResult;
import org.pcap4j.core.NotOpenException;
import org.pcap4j.core.PcapNativeException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.Map;

@RestController
public class CommandController {
    @Autowired
    IdsManager idsManager;

    @GetMapping("/test")
    public JsonResult test(@RequestParam Map<String, Object> param){
        System.out.println(param);
        System.out.println(idsManager.getStatus());
        return JsonResult.success("success");
    }


    @GetMapping("/start")
    public JsonResult start(@RequestParam Map<String, Object> param) throws NotOpenException, PcapNativeException, IOException, InterruptedException {
        System.out.println(param);
        idsManager.start();
        return JsonResult.success("success");
    }

    @GetMapping("/status")
    public JsonResult status(@RequestParam Map<String, Object> param) throws NotOpenException, PcapNativeException, IOException, InterruptedException {
        System.out.println(param);
//        idsManager.start();
        return JsonResult.success(idsManager.getStatus());
    }

    @GetMapping("/stop")
    public JsonResult stop(@RequestParam Map<String, Object> param) throws PcapNativeException, IOException, InterruptedException {
        System.out.println(param);
        try {
            idsManager.close();
        } catch (NotOpenException e) {
            return JsonResult.error("idsManager is closed");
        }
        return JsonResult.success(idsManager.getStatus());
    }

    @GetMapping("/pool-status")
    public JsonResult poolStatus(){
        return JsonResult.success(idsManager.getThreadPoolStatus());
    }

    /**
     * 获取抓包状况
     */
    @GetMapping("/packet-capture-status")
    public JsonResult status() throws NotOpenException, PcapNativeException {
        Map<String, Object> packetInfo = idsManager.sniffer.getPacketInfo();
        return JsonResult.success(packetInfo);
    }
}
